package mx.vlabs.soapdroid.wsdl2java.ui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.BoxLayout;

import mx.vlabs.soapdroid.wsdl2java.ui.exceptions.TaskExecutionException;
import mx.vlabs.soapdroid.wsdl2java.ui.exceptions.WizardPanelDataValidationException;

import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;

public class NavigationPanel extends JPanel {

	/**
	 * Create the panel.
	 */
	
	private int currentWizardPanelIndex = 0;

	private WizardPanel[] wizardPanels;
	private WizardPanel currentWizardPanel;
	private MainFrame parent;
	private JButton btnPrevious,btnNext,btnGenerate;
	
	
	public NavigationPanel(WizardPanel[] wizardPanels,MainFrame parent) {
		
		if(wizardPanels==null || wizardPanels.length==0) 
			throw new RuntimeException("no wizard panels loaded");
		
		this.wizardPanels = wizardPanels;
		this.parent = parent;
		
		setLayout(new GridLayout(1, 0, 0, 0));
		
		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		add(btnClose);
		
		btnPrevious = new JButton("Previous");
		btnPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				currentWizardPanel.onPrevious(NavigationPanel.this);
				displayWizardPanel(currentWizardPanelIndex-1);
			}
		});
		btnPrevious.setEnabled(false);
		add(btnPrevious);
		
		btnNext = new JButton("Next");
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new AsyncTask() {
					
					@Override
					public void onSuccess() {
						// TODO Auto-generated method stub
						displayWizardPanel(currentWizardPanelIndex+1);
					}
					
					@Override
					public void onError(TaskExecutionException e) {
						// TODO Auto-generated method stub
						JOptionPane.showMessageDialog
						(
							NavigationPanel.this, 
						    e.getMessage(), 
						    "Validation Error", 
						    JOptionPane.ERROR_MESSAGE
						    
						 );
						
						getBtnNext().setEnabled(true);
					}
					
					@Override
					public void onComplete() {
						// TODO Auto-generated method stub
						
						NavigationPanel.this.parent.closeLoaderWindow();
					}
					
					@Override
					public void execute() throws TaskExecutionException {
						// TODO Auto-generated method stub
						try {
							currentWizardPanel.onNext(NavigationPanel.this);
						} catch (WizardPanelDataValidationException e) {
							// TODO Auto-generated catch block
							throw new TaskExecutionException(e);
						}
					}
					
					@Override
					public void beforeExecute() {
						// TODO Auto-generated method stub
						NavigationPanel.this.parent.openLoaderWindow();
						getBtnGenerate().setEnabled(false);
						getBtnPrevious().setEnabled(false);
						getBtnNext().setEnabled(false);
					}
				};
				
				
			}
		});
		add(btnNext);
		
		btnGenerate = new JButton("Generate!");
		btnGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				WSClientGeneratorPanel panel = (WSClientGeneratorPanel) currentWizardPanel;
				
				boolean validated = true;
				
				if(panel.getTxtPackageName().getText().equals("")){
					JOptionPane.showMessageDialog(
							NavigationPanel.this.parent, 
							"Please select a package name", "Error", 
							JOptionPane.ERROR_MESSAGE);
					
					validated = false;
				}else if(panel.getTxtProjectLocation().getText().equals("")){
					JOptionPane.showMessageDialog(
							NavigationPanel.this.parent, 
							"Please select a project location", "Error", 
							JOptionPane.ERROR_MESSAGE);
					validated = false;
				}else{
					
					File file = new File(panel.getTxtProjectLocation().getText());
					
					if(!file.exists())
					validated = false;
					
				}
				
				if(validated){
				
					int op = JOptionPane.showConfirmDialog(
								NavigationPanel.this.parent, 
								"Generating the client, will overwrite already existing client files, Are you sure?", 
								"Warning", 
								JOptionPane.WARNING_MESSAGE
					);
					
					if(op == JOptionPane.YES_OPTION){
						
						new AsyncTask() {
							
							@Override
							public void onSuccess() {
								// TODO Auto-generated method stub
								JOptionPane.showMessageDialog(
										NavigationPanel.this.parent, 
										"WS Client Java files successfully generated", "Success!", 
										JOptionPane.INFORMATION_MESSAGE);
							}
							
							@Override
							public void onError(TaskExecutionException e) {
								// TODO Auto-generated method stub
								JOptionPane.showMessageDialog(
										NavigationPanel.this.parent, 
										e.getMessage(), "Error", 
										JOptionPane.ERROR_MESSAGE);
							}
							
							@Override
							public void onComplete() {
								// TODO Auto-generated method stub
								NavigationPanel.this.parent.closeLoaderWindow();
							}
							
							@Override
							public void execute() throws TaskExecutionException {
								// TODO Auto-generated method stub
								try {
									NavigationPanel.this.parent.getWsdl2Java().buildArtifacts();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									throw new TaskExecutionException(e);
								}
							}
							
							@Override
							public void beforeExecute() {
								// TODO Auto-generated method stub
								WSClientGeneratorPanel wsp = (WSClientGeneratorPanel) NavigationPanel.this.currentWizardPanel;
								
								NavigationPanel.this.parent.getWsdl2Java().setPackageName(wsp.getTxtPackageName().getText());
								NavigationPanel.this.parent.getWsdl2Java().setOutputPath(wsp.getTxtProjectLocation().getText());
								NavigationPanel.this.parent.openLoaderWindow("Building WS Client files...");
							}
						};
						
					}
				
				}
				
			}
		});
		btnGenerate.setEnabled(false);
		add(btnGenerate);
		
		displayWizardPanel(0);

	}
	
	public void displayWizardPanel(int index){
		if(currentWizardPanel!=null)
		parent.getPnlMain().remove(currentWizardPanel);
		parent.repaint(100);
		parent.getPnlMain().add(wizardPanels[index]);
		currentWizardPanelIndex = index;
		currentWizardPanel = wizardPanels[index];
		currentWizardPanel.init();
		parent.getPnlMain().revalidate();
	}

	public JButton getBtnPrevious() {
		return btnPrevious;
	}

	public JButton getBtnNext() {
		return btnNext;
	}

	public JButton getBtnGenerate() {
		return btnGenerate;
	}
	
	

}
